//以下为修改jQuery Validation插件兼容Bootstrap的方法，没有直接写在插件中是为了便于插件升级
$.validator.setDefaults({
    highlight: function (element) {
        $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
    },
    success: function (element) {
        element.closest('.form-group').removeClass('has-error').addClass('has-success');
    },
    errorElement: "span",
    errorPlacement: function (error, element) {
        if (element.is(":radio") || element.is(":checkbox")) {
            error.appendTo(element.parent().parent().parent());
        } else {
            error.appendTo(element.parent());
        }
    },
    errorClass: "help-block m-b-none",
    validClass: "help-block m-b-none"
});

var formValidater;
//以下为官方示例
$().ready(function () {
    // validate signup form on keyup and submit
    var icon = "<i class='fa fa-times-circle'></i> ";
    formValidater = $("#signupForm").validate({
        rules: {
            username: {
                required: true,
                minlength: 2
            },
            password: {
                required: true,
                minlength: 5
            },
            confirm_password: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            phone: {
                required: true,
                minlength: 11,
                maxlength: 11,
            },
            code: {
                    required: true,
                    minlength: 6,
                    maxlength: 6,
                }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名",
                minlength: icon + "用户名必须两个字符以上"
            },
            password: {
                required: icon + "请输入您的密码",
                minlength: icon + "密码必须5个字符以上"
            },
            confirm_password: {
                required: icon + "请再次输入密码",
                minlength: icon + "密码必须5个字符以上",
                equalTo: icon + "两次输入的密码不一致"
            },
            phone: {
                   required: icon + "请输入手机号码",
                   minlength: icon + "手机号码必须是11位",
                   maxlength: icon + "手机号码必须是11位"
                },
            code: {
                       required: icon + "请输入短信验证码",
                       minlength: icon + "验证码必须是6位",
                       maxlength: icon + "验证码必须是6位"
                    }
        }
    });

    $('#loadingButton').click(function() {
        var flag = formValidater.element("#phone");
        if (flag) {
            $(this).removeClass('btn-primary').addClass('disabled');
            $(this).html('<span class="red">59</span> 秒后重新获取');
            var secondObj = $('#loadingButton').find('span');
            var secondObjVal = secondObj.text();

            function secondCounter() {
                var secondTimer = setTimeout(function() {
                    secondObjVal--;
                    secondObj.text(secondObjVal);
                    secondCounter();
                }, 1000);
                if (secondObjVal == 0) {
                    clearTimeout(secondTimer);
                    $('#loadingButton').text('重新获取校验码');
                    $('#loadingButton').removeClass('disabled').addClass('btn-primary');
                }
            }
            secondCounter();

            $.ajax({
                url: $("#signupForm").attr("action")+'Code',
                data: {'phone':$("#phone").val()},
                type: "POST",
                dataType: "json",
                success: function(result){
                }
              });
        }
    })

    $("#submit").click(function() {
        $.ajax({
            url: $("#signupForm").attr("action"),
            data: $("#signupForm").serialize(),
            type: "POST",
            dataType: "json",
            success: function(result){
                if(result.code == 1){
                    alert("注册成功");
                    window.location.href=BASE_PATH+"/sso/index";
                }else{
                    alert(result.data);
                }
            },
            error:function(){
                alert("网络异常");
            }
          });
          return false;
    });

    // propose username by combining first- and lastname
/*    $("#username").focus(function () {
        var firstname = $("#firstname").val();
        var lastname = $("#lastname").val();
        if (firstname && lastname && !this.value) {
            this.value = firstname + "." + lastname;
        }
    });*/
});
